package com.szpx.controller;

import com.github.pagehelper.PageInfo;
import com.szpx.entity.Goods;
import com.szpx.entity.Result;
import com.szpx.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author June
 * @version 1.0.0
 * @title GoodsController
 * @create 2024/3/28 16:42
 * @description
 **/
@RequestMapping("/main/goods")
@RestController
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询所有商品信息
     *
     * @return
     */
    @GetMapping
    public Result<List<Goods>> findAll() {
        List<Goods> goodsList = goodsService.findAll();
        return Result.success(goodsList);
    }
    
    /**
     * 根据id来查询商品信息
     *
     * @param id
     * @return
     */
    @GetMapping("/search/{id}")
    public Result<Goods> findById(@PathVariable(value = "id") Integer id) {
        return  Result.success(goodsService.findById(id));
    }

    /**
     * 添加商品信息
     *
     * @param goods
     */
    @PostMapping("/add")
    public Result addGoods(@RequestBody Goods goods) {
        goodsService.addGoods(goods);
        return Result.successnoData();
    }

    /**
     * 修改商品信息
     *
     * @param goods
     */
    @PutMapping("/{id}")
    public Result updateGoods(@RequestBody Goods goods, @PathVariable Integer id) {
        Goods goods1 = goodsService.findById(id);
        if (goods.getUrl() != null) {
            goods1.setUrl(goods.getUrl());
        }
        if (goods.getPrice() != null) {
            goods1.setPrice(goods.getPrice());
        }
        if (goods.getTitle() != null) {
            goods1.setTitle(goods.getTitle());
        }
        if (goods.getShop() != null) {
            goods1.setShop(goods.getShop());
        }
        if (goods.getType() != null) {
            goods1.setType(goods.getType());
        }
        goodsService.updateGoods(goods1);
        return Result.success(goods1);
    }

    /**
     * 删除商品信息
     *
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteGoods(@PathVariable Integer id) {
        goodsService.deleteGoods(id);
        return Result.successnoData();
    }

    /**
     * 多条件查询商品信息
     *
     * @param goods
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Goods>> findList(@RequestBody Goods goods) {
        List<Goods> list = goodsService.findList(goods);
        return Result.success(list);
    }

    /**
     * 分页查询商品信息
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo<Goods>> findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<Goods> pageInfo = goodsService.findPage(page, size);
        return Result.success(pageInfo);
    }

    /**
     * 多条件分页查询
     *
     * @param goods
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Goods>> findPage(@RequestBody Goods goods, @PathVariable int page, @PathVariable int size) {
        PageInfo<Goods> pageInfo = goodsService.findPage(goods, page, size);
        return Result.success(pageInfo);
    }
}
